package com.yuezhi.mylauncher.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuezhi.mylauncher.R;
import com.yuezhi.mylauncher.base.GeneralParameter;
import com.yuezhi.mylauncher.utils.AdbUtils;
import com.yuezhi.mylauncher.utils.AppUtils;
import com.yuezhi.mylauncher.utils.ToastUtil;
import com.yuezhi.mylauncher.utils.YXApiUtils;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView ivClose;
    private TextView tvSetCustomLauncher, tvSetSystemLauncher,tvCurrentLauncher;
    private TextView tvSetHideStatusBar, tvSetShowStatusBar,tvCurrentStatusBarStatus;
    private TextView tvOpenSetting;
    private TextView tvShutDown, tvReboot;
    private final String CUSTOM_LAUNCHER = "com.yuezhi.mylauncher";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppUtils.fullScreen(SettingActivity.this);
        setContentView(R.layout.activity_setting);

        initView();
        initClick();
    }

    private void initView() {
        ivClose = findViewById(R.id.iv_close);
        tvSetCustomLauncher = findViewById(R.id.tv_set_custom_launcher);
        tvSetSystemLauncher = findViewById(R.id.tv_set_system_launcher);
        tvCurrentLauncher = findViewById(R.id.tv_current_launcher);
        tvSetHideStatusBar = findViewById(R.id.tv_set_hide_status_bar);
        tvSetShowStatusBar = findViewById(R.id.tv_set_show_status_bar);
        tvCurrentStatusBarStatus = findViewById(R.id.tv_current_status_bar_status);
        tvOpenSetting = findViewById(R.id.tv_open_setting);
        tvShutDown = findViewById(R.id.tv_shut_down);
        tvReboot = findViewById(R.id.tv_reboot);

        tvCurrentLauncher.postDelayed(() -> {
            String packageName = AppUtils.getLauncherPackageName(SettingActivity.this);
            if (TextUtils.isEmpty(packageName)) {
                tvCurrentLauncher.setText("使用系统桌面中");
            }else {
                if (packageName.equals(CUSTOM_LAUNCHER)) {
                    tvCurrentLauncher.setText("禁用系统桌面中");
                }else {
                    tvCurrentLauncher.setText("使用系统桌面中");
                }
            }
        },500);
        tvCurrentStatusBarStatus.postDelayed(() -> {
            boolean navBarIsHide = YXApiUtils.init().getNavBarIsHide();
            if (navBarIsHide) {
                tvCurrentStatusBarStatus.setText("隐藏系统状态栏");
            }else {
                tvCurrentStatusBarStatus.setText("显示系统状态栏");
            }
        },1000);
    }

    private void initClick() {
        ivClose.setOnClickListener(this);
        tvSetCustomLauncher.setOnClickListener(this);
        tvSetSystemLauncher.setOnClickListener(this);
        tvSetHideStatusBar.setOnClickListener(this);
        tvSetShowStatusBar.setOnClickListener(this);
        tvOpenSetting.setOnClickListener(this);
        tvShutDown.setOnClickListener(this);
        tvReboot.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.iv_close:
                    SettingActivity.this.finish();
                    break;
                case R.id.tv_set_custom_launcher:
                    ToastUtil.showMessage("设置桌面为自定义桌面");
                    String launcherPackageName;
                    String phoneModel = AppUtils.getPhoneModel();
                    if (!TextUtils.isEmpty(phoneModel)) {
                        if (phoneModel.equals(GeneralParameter.SMT_RK3288)) {
                            launcherPackageName = "com.hdeva.launcher";
                        } else {//其他班牌
                            launcherPackageName = "com.android.launcher3";
                        }
                    } else {
                        launcherPackageName = "com.android.launcher3";
                    }
                    AdbUtils.runRootCmd(String.format("pm disable-user %s",launcherPackageName));
                    if (tvCurrentLauncher!=null) {
                        tvCurrentLauncher.postDelayed(() -> {
                            String packageName = AppUtils.getLauncherPackageName(SettingActivity.this);
                            if (TextUtils.isEmpty(packageName)) {
                                tvCurrentLauncher.setText("使用系统桌面中");
                            }else {
                                if (packageName.equals(CUSTOM_LAUNCHER)) {
                                    tvCurrentLauncher.setText("禁用系统桌面中");
                                }else {
                                    tvCurrentLauncher.setText("使用系统桌面中");
                                }
                            }
                        },2000);
                    }
                    break;
                case R.id.tv_set_system_launcher:
                    ToastUtil.showMessage("设置桌面为系统桌面");
                    String launcherName;
                    String phoneModels = AppUtils.getPhoneModel();
                    if (!TextUtils.isEmpty(phoneModels)) {
                        if (phoneModels.equals(GeneralParameter.SMT_RK3288)) {
                            launcherName = "com.hdeva.launcher";
                        } else {//其他班牌
                            launcherName = "com.android.launcher3";
                        }
                    } else {
                        launcherName = "com.android.launcher3";
                    }
                    AdbUtils.runRootCmd(String.format("pm enable %s",launcherName));
                    if (tvCurrentLauncher!=null) {
                        tvCurrentLauncher.postDelayed(() -> {
                            String packageName = AppUtils.getLauncherPackageName(SettingActivity.this);
                            if (TextUtils.isEmpty(packageName)) {
                                tvCurrentLauncher.setText("使用系统桌面中");
                            }else {
                                if (packageName.equals(CUSTOM_LAUNCHER)) {
                                    tvCurrentLauncher.setText("禁用系统桌面中");
                                }else {
                                    tvCurrentLauncher.setText("使用系统桌面中");
                                }
                            }
                        },2000);
                    }
                    break;
                case R.id.tv_set_hide_status_bar:
                    ToastUtil.showMessage("隐藏状态栏");
                    YXApiUtils.init().hideNavBar(true);
                    YXApiUtils.init().setSlideShowNotificationBar(false);
                    YXApiUtils.init().setSlideShowNavBar(false);
                    tvCurrentStatusBarStatus.postDelayed(() -> {
                        boolean navBarIsHide = YXApiUtils.init().getNavBarIsHide();
                        if (navBarIsHide) {
                            tvCurrentStatusBarStatus.setText("隐藏系统状态栏");
                        }else {
                            tvCurrentStatusBarStatus.setText("显示系统状态栏");
                        }
                    },1000);
                    break;
                case R.id.tv_set_show_status_bar:
                    ToastUtil.showMessage("显示状态栏");
                    YXApiUtils.init().hideNavBar(false);
                    YXApiUtils.init().setSlideShowNotificationBar(true);
                    YXApiUtils.init().setSlideShowNavBar(true);
                    tvCurrentStatusBarStatus.postDelayed(() -> {
                        boolean navBarIsHide = YXApiUtils.init().getNavBarIsHide();
                        if (navBarIsHide) {
                            tvCurrentStatusBarStatus.setText("隐藏系统状态栏");
                        }else {
                            tvCurrentStatusBarStatus.setText("显示系统状态栏");
                        }
                    },1000);
                    break;
                case R.id.tv_open_setting:
                    ToastUtil.showMessage("打开系统设置");
                    AppUtils.openSetting();
                    break;
                case R.id.tv_shut_down:
                    ToastUtil.showMessage("设备关机");
                    YXApiUtils.init().shutDown();
                    break;
                case R.id.tv_reboot:
                    ToastUtil.showMessage("设备重启");
                    YXApiUtils.init().reboot();
                    break;
            }
        }
    }
}